package chu.dejian.dialogOrMenu;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.text.DateFormatSymbols;
import java.util.Calendar;

import chu.dejian.alarm.Alarm;
import com.sam.blemodule.R;
import chu.dejian.dialogOrMenu.CalendarDialog;
import com.sam.ui.base.fragment.BaseFragment;
import com.sam.utils.T;
import com.sam.widget.headerBar.BaseHeaderView;
import com.sam.widget.headerBar.LeftImgTitleRightImgBar;
import com.sam.widget.headerBar.LeftImgTitleRightTextBar;

public class AlarmRepeatDialog extends Dialog {
    private Context context;
    private LeftImgTitleRightTextBar header;
    private ListView alarmRepeatListView;
    Alarm.DaysOfWeek newDaysOfWeek,daysOfWeek;// = new Alarm.DaysOfWeek(0);

    public AlarmRepeatDialog(Context context) {
        super(context,R.style.DialogTheme);
        this.context = context;
        setContentView(R.layout.fragment_measure_alarm_repeat); // 最好放在这两句中间
        setCanceledOnTouchOutside(true);// 点击对话框外部取消对话框显示
        WindowManager.LayoutParams lp = getWindow().getAttributes();
//		getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);// 添加模糊效果 ---- 不但没有模糊效果，窗口四个角还有黑色，fuck
        // 设置透明度，对话框透明(包括对话框中的内容)alpha在0.0f到1.0f之间。1.0完全不透明，0.0f完全透明
//		lp.alpha = 1.0f;
        lp.dimAmount = 0.5f;// 设置对话框显示时的黑暗度，0.0f和1.0f之间，在我这里设置成0.0f会出现黑屏状态
        getWindow().setAttributes(lp);

        findView();
        initView();
    }

    public void findView() {
        header = (LeftImgTitleRightTextBar) findViewById(R.id.header);
        alarmRepeatListView = (ListView) findViewById(R.id.measure_alarm_repeat);
    }

    public void initView() {
        daysOfWeek = new Alarm.DaysOfWeek(0);
        newDaysOfWeek = daysOfWeek;
        header.setOnHeaderClickListener(onHeaderClickListener);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,R.layout.row_alarm_repeat_time);
        adapter.addAll(context.getResources().getStringArray(R.array.clock_repeat_time));
        alarmRepeatListView.setAdapter(adapter);
        alarmRepeatListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        alarmRepeatListView.setOnItemClickListener(onItemClickListener);
    }

    public void setRepeatDaysValue(Alarm.DaysOfWeek days) {
        daysOfWeek.set(days);
        newDaysOfWeek = days;
        for(int i = 0; i < 7; i++)
        alarmRepeatListView.setItemChecked(i,daysOfWeek.isSet(i));
    }

    private BaseHeaderView.OnHeaderClickListener onHeaderClickListener = new BaseHeaderView.OnHeaderClickListener() {
        @Override
        public void onHeaderLeftClicked() {
            dismiss();
        }

        @Override
        public void onHeaderRightClicked() {
            daysOfWeek = newDaysOfWeek;
            dismiss();
        }
    };

    public Alarm.DaysOfWeek getDaysOfWeek(){
        return daysOfWeek;
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            boolean isSet = newDaysOfWeek.isSet(position);
            alarmRepeatListView.setItemChecked(position,!isSet);
            newDaysOfWeek.set(position,!isSet);
            /*switch (position){
                case 0:
//                    T.showShort(getActivity(),"星期一");
                    break;
                case 1:
//                    T.showShort(getActivity(),"星期二");
                    break;
                case 2:
//                    T.showShort(getActivity(),"星期三");
                    break;
                case 3:
//                    T.showShort(getActivity(),"星期四");
                    break;
                case 4:
//                    T.showShort(getActivity(),"星期五");
                    break;
                case 5:
//                    T.showShort(getActivity(),"星期六");
                    break;
                case 6:
//                    T.showShort(getActivity(),"星期日");
                    break;
                case 7:
//                    T.showShort(getActivity(),"每月");
                    if(calendarDialog == null){
                        calendarDialog = new CalendarDialog(getActivity());
                    }
                    calendarDialog.show();
                    break;
            }*/
        }
    };

}
